<?php


namespace app\model;


use think\facade\Db;

class FortifyModel
{

    public static function scan()
    {
        while (true) {
            $mtime = date("Y-m-d H:i:s", strtotime("-1 hour"));
            $list = Db::table('code')
                ->whereTime('time', '<', $mtime)
                ->select()
                ->toArray();

            //遍历仓库信息($list as $codeInfo){

            foreach ($list as $x => $y) {
//                var_dump($y["url"]);
                $project_name = $y['name'];

                //1. 拉取代码
                $codePath = '/root/file';
                if (file_exists("/root/file/{$project_name}") == false) {

                    $cmd = "cd {$codePath}/ && git clone --depth=1 {$y["url"]}  {$project_name}";
                    system($cmd);
                }

//               var_dump($cmd);


                //2. 扫描代码

                $out = "/root/file/{$project_name}";
                self::startScan($codePath, $out);

                //4. 存储结果
                FortifyModel::chenting($y['id'], $out . ".xml");

                //5. 更新时间

                $time = date('Y-m-d H:i:s', time()); // 2018-10-3 15:57:05
                $all3 = Db::table('code')
                    ->update(['id' => $y['id'], 'time' => $time]);
            }

            print_r("fortify扫描完成,休息10秒钟...");
            sleep(10);
        }
    }

    public static function xmlToArray($xml)
    {
        libxml_disable_entity_loader(true);
        $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $values;
//ReportSection[2],SubSection,IssueListing,Chart

    }

    public static function chenting($cid, $file_name)
    {

        $file_connect = file_get_contents($file_name);

        $san = FortifyModel::xmlToArray($file_connect);
        $count = $san['ReportSection'][2]['SubSection']['IssueListing']['Chart']['GroupingSection'];

        foreach ($count as $value) {

            if (isset($value['Issue'][0])) {
                foreach ($value['Issue'] as $v2) {

                    foreach ($v2 as &$v3) {
                        $v3 = is_array($v3) ? json_encode($v3) : $v3;
                    }
                    unset($v2['@attributes']);
                    $v2['cid'] = $cid;
                    Db::name('suibian')->insert($v2);

                }
            } else {
                foreach ($value as &$v3) {
                    $v3 = is_array($v3) ? json_encode($v3) : $v3;
                }
                unset($value['@attributes']);
                $value['cid'] = $cid;
                Db::name('suibian')->insert($value);
            }

        }

    }

    public static function startScan($codePath, $outPath)
    {

        $buildId = md5($codePath);

        if (file_exists($outPath)) {
//            chmod($outPath, 0777, true);
        }
//        fortify存放路径

        $fortifyPath = "/root/fortifyLinux/Fortify_SCA_and_Apps_20.2.0";
        $base = "cd {$fortifyPath}/bin && ";
        $cmd = $base . "./sourceanalyzer -b {$buildId} -clean";
        system($cmd);

        if (file_exists("{$outPath}.fpr") == false) {
            $cmd = $base . "./sourceanalyzer -b {$buildId} -Xmx4096M -Xms2048M -Xss48M     -source 1.8 -machine-output   {$codePath}";
            system($cmd);
            $cmd = $base . "./sourceanalyzer -b {$buildId} -scan -format fpr    -f {$outPath}.fpr -machine-output ";
            $cmd .= " -no-default-rules  -rules  {$fortifyPath}/Core/config/rules/core_php.bin";
            system($cmd);
        }

        if (file_exists("{$outPath}.xml") == false) {
            $cmd = $base . "./ReportGenerator  -format xml -f {$outPath}.xml -source {$outPath}.fpr -template DeveloperWorkbook.xml";
            system($cmd);
        }
    }
}